package com.my.ads.lib.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.my.ads.lib.AdSummary;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by xuanyang on 2018/3/6.
 */

public class BaseActivity extends AppCompatActivity {

    String interstitialAd = "interstitialAd";
    String showTime = "showTime";
    int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AdSummary.getInstance().cacheAllAd(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        int time = 8000;
        if (flag == 0) {
            flag = 1;
            time = 8000;
        } else if (flag == 1) {
            flag = 2;
            time = 12000;
        } else if (flag == 2) {
            flag = 3;
            time = 15000;
        } else {
            time = 15000;
        }

        resumeAd(time);
    }

    private void resumeAd(int time) {
        SharedPreferences sp = getSharedPreferences(interstitialAd, Context.MODE_PRIVATE);
        Long show_time_ad = sp.getLong(showTime, 0L);

        SharedPreferences.Editor editor = sp.edit();

        if ((System.currentTimeMillis() - show_time_ad >= time)) {
            AdSummary.getInstance().showInterstitialAd(this, "");

            editor.putLong(showTime,System.currentTimeMillis());
            editor.apply();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


}
